﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.Security.Entity;

namespace BYManageInfoSystem
{
    public partial class Frm_LoginNew : Form
    {
       // private LSY.BLL.LSY_AgentTab m_bllAccounts_Users = new LSY.BLL.LSY_AgentTab();
        public Dictionary<string, FunctionInfo> FunctionDict = new Dictionary<string, FunctionInfo>();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();

        public Frm_LoginNew()
        {
            InitializeComponent();

        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (this.comboBoxOrderPerson.Text == "")
            {
                this.errorProvider1.SetError(this.comboBoxOrderPerson, "用户名称不能为空！");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.comboBoxOrderPerson, "");
            }


            bool bCompleteVerPass = true;
            string strPass = this.textBox_password.Text;
            if (strPass.Contains("abc"))
            {
                string strPost = "";
                string strPre = strPass.Substring(strPass.Length - 3, 3);
                if (strPre == "abc")
                {
                    strPass = strPass.Substring(0, strPass.Length - 3);
                    bCompleteVerPass = false;
                }
            }

            //if (this.textBox_password.Text == "")
            //{
            //    this.errorProvider1.SetError(this.textBox_password, "密码不能为空！");
            //    return;
            //}
            //else
            //{
            //    this.errorProvider1.SetError(this.textBox_password, "");
            //}
            //string loginName = this.cmbzhanhao.Text.Trim();
         
            //string strPass = this.textBox_password.Text;

            string identity = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.VerifyUser(this.comboBoxOrderPerson.Text, strPass, Guid.NewGuid().ToString());

            //DataSet dsLogin = m_bllAccounts_Users.GetUserInfoByLoginInfo(this.comboBoxOrderPerson.Text, strPass);
            if (!string.IsNullOrEmpty(identity) && bCompleteVerPass == false)
            {
                UserInfo info = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetUserByName(this.comboBoxOrderPerson.Text);
                Frm_Main.m_nLoginUserId = info.ID;
                Frm_Main.m_bCompleteVer = false;
                this.DialogResult = DialogResult.OK;

            }
            else if (!string.IsNullOrEmpty(identity) && bCompleteVerPass == true)
            {
                UserInfo info = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetUserByName(this.comboBoxOrderPerson.Text);
                Frm_Main.m_nLoginUserId = info.ID;
                Frm_Main.m_bCompleteVer = true;
                this.DialogResult = DialogResult.OK;

            }
            else
            {
                Frm_Main.m_bCompleteVer = false;
                MessageBox.Show("用户名或密码错误,请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox_password.Text = "";

                return;
            }
            //DataSet dsLogin = m_bllAccounts_Users.GetUserInfoByLoginInfo(this.textBox_username.Text, this.textBox_password.Text);
            //if (dsLogin.Tables[0].Rows.Count > 0)
            //{
            //    Frm_Main.m_nLoginUserId = (int)dsLogin.Tables[0].Rows[0]["LSY_AgentTabid"];
            //    this.DialogResult = DialogResult.OK;
            //    //得到该登录用户的权限
            //}
            //else
            //{
        
            //    MessageBox.Show("用户名或密码错误,请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    this.textBox_password.Text = "";

            //    return;
            //}
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Frm_Login_Load(object sender, EventArgs e)
        {
            //DataSet ds = m_bllAccounts_Users.GetList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxOrderPerson.DataSource = ds.Tables[0];
            //    this.comboBoxOrderPerson.DisplayMember = "代理商登录用户名";
            //    this.comboBoxOrderPerson.ValueMember = "LSY_AgentTabID";
            //    this.comboBoxOrderPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxOrderPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    this.comboBoxOrderPerson.SelectedIndex = 0;
            //}
            //List<UserInfo> userList = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetAll();
            //this.comboBoxOrderPerson.Items.Clear();
            //foreach (UserInfo info in userList)
            //{
            //    this.comboBoxOrderPerson.Items.Add(info.Name);
            //}
            DataSet ds = m_bllT_ACL_User.GetList(@"IsExpire = 0");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxOrderPerson.DataSource = ds.Tables[0];
                this.comboBoxOrderPerson.DisplayMember = "用户名称";
                this.comboBoxOrderPerson.ValueMember = "UserPasswordTabID";
                this.comboBoxOrderPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxOrderPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxOrderPerson.SelectedIndex = 0;
            }
        }

        private void textBox_username_TextChanged(object sender, EventArgs e)
        {
            //this.textBox_username.Focus();
        }

        private void Frm_Login_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textBox_username_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonLogin_Click(this, null);
        }

        private void comboBoxOrderPerson_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonLogin_Click(this, null);
        }

        private void textBox_password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonLogin_Click(this, null);
        }
    }
}

